package video

import (
	"context"
	"encoding/json"
	"fmt"

	"app-api/app/internal/svc"
	"app-api/app/internal/types"
	"app-api/clients/video"

	"github.com/zeromicro/go-zero/core/logx"
)

type PostVideoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPostVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PostVideoLogic {
	return &PostVideoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PostVideoLogic) PostVideo(req *types.Video) (resp *types.Empty, err error) {
	userID, _ := l.ctx.Value("id").(json.Number).Int64()
	fmt.Println("userID:", userID)
	_, err = l.svcCtx.VideoRPC.AddVideo(l.ctx, &video.AddVideoRequest{
		UserId:   userID,
		Title:    req.Title,
		PlayUrl:  req.PlayURL,
		CoverUrl: req.CoverURL,
	})
	fmt.Println("err:", err)
	if err != nil {
		return nil, err
	}
	return &types.Empty{}, nil
}
